package com.lc.protocol;

import android.util.Log;

import com.lc.tool.Convert;

/**
 * Created by Zhyioo on 2017/3/24.
 */

public class HardwareAdapter implements IInteractiveTrans, ITimeoutBehavior {

    protected static String LogTitle = "HardwareAdapter";

    protected ITransmitHandler _handler = null;

    protected int _waitTimeoutMs = 10000;

    protected int _waitIntervalMs = 20;

    protected boolean _isCancel = false;

    public HardwareAdapter() {}

    public HardwareAdapter(ITransmitHandler handler) {
        _handler = handler;
    }

    public boolean Select(ITransmitHandler handler) {
        if(handler == null) {
            return false;
        }
        _handler = handler;
        return true;
    }

    public boolean IsValid() {
        return _handler != null;
    }

    public void Cancel() {
        _isCancel = true;
    }

    public void Reset() {
        _isCancel = false;
    }

    @Override
    public boolean Write(byte[] data, int datalen) {
        if(!IsValid()) {
            Log.e(LogTitle, "Handler无效");
            return false;
        }
        String sTimeout = "等待超时:" + _waitTimeoutMs + ",轮询间隔:" + _waitIntervalMs;
        Log.i(LogTitle, sTimeout);

        long currentTime = System.currentTimeMillis();
        long time = currentTime;

        String sData = Convert.HexToString(data, datalen);
        Log.i(LogTitle, "待写入数据:" + sData);

        while(true) {
            if(_isCancel) {
                Log.e(LogTitle, "写入操作被取消");
                return false;
            }

            if(_handler.OnWrite(data, datalen, _waitIntervalMs)) {
                return true;
            }
            currentTime = System.currentTimeMillis();
            if((currentTime - time) > _waitTimeoutMs) {
                Log.e(LogTitle, "写入数据超时");
                break;
            }

            try {
                Thread.sleep(_waitIntervalMs);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }

        return false;
    }

    @Override
    public boolean Read(byte[] data, int[] datalen) {
        if(!IsValid()) {
            Log.i(LogTitle, "Handler无效");
            return false;
        }

        String sTimeout = "等待超时:" + _waitTimeoutMs + ",轮询间隔:" + _waitIntervalMs;
        Log.i(LogTitle, sTimeout);

        long currentTime = System.currentTimeMillis();
        long time = currentTime;

        while(true) {
            if(_isCancel) {
                Log.e(LogTitle, "读取操作被取消");
                return false;
            }

            if(_handler.OnRead(data, datalen, _waitIntervalMs)) {
                String sData = Convert.HexToString(data, datalen[0]);
                Log.i(LogTitle, "读取到数据:" + sData);
                return true;
            }
            currentTime = System.currentTimeMillis();
            if((currentTime - time) > _waitTimeoutMs) {
                Log.e(LogTitle, "读取数据超时");
                break;
            }

            try {
                Thread.sleep(_waitIntervalMs);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }

        return false;
    }

    @Override
    public int SetWaitTimeout(int timeoutMs) {
        return _waitTimeoutMs = timeoutMs;
    }

    @Override
    public int SetOperatorInterval(int timeoutMs) {
        return _waitIntervalMs = timeoutMs;
    }
}
